package com.jblv.frame.web.facade.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.jblv.frame.common.dto.PageResult;
import com.jblv.frame.common.enums.ExceptionEnum;
import com.jblv.frame.common.exception.FrameException;
import com.jblv.frame.common.utils.RequestHolder;
import com.jblv.frame.common.utils.SortListUtils;
import com.jblv.frame.common.utils.StringUtils;
import com.jblv.frame.common.utils.bean.BeanUtils;
import com.jblv.frame.common.utils.tree.TreeUtils;
import com.jblv.frame.user.api.bll.ISysDeptBLL;
import com.jblv.frame.user.api.vo.SysDeptListVO;
import com.jblv.frame.user.api.dto.SysDeptDTO;
import com.jblv.frame.user.api.entity.SysDept;
import com.jblv.frame.user.api.params.SysDeptListParams;
import com.jblv.frame.user.api.params.SysDeptParams;
import com.jblv.frame.web.facade.ISysDeptFacade;
import com.jblv.frame.web.utils.DeptTreeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title SysDeptFacade
 * @projectName jblv-frame
 * @packageName com.jblv.web.facade.impl
 * @description 部门信息门面实现类
 * @date Create at 2020/5/26 22:17
 * @since jdk1.8
 */
@Service
public class SysDeptFacade implements ISysDeptFacade {

    private static final Logger logger = LoggerFactory.getLogger(SysDeptFacade.class);

    // @Autowired
    @Reference(check = false)
    ISysDeptBLL sysDeptBLL;

    @Override
    public SysDept get(String id){
        return sysDeptBLL.get(id);
    }

    @Override
    public PageResult<SysDeptDTO> getList() {
        List<SysDeptDTO> listDTO = new ArrayList<>();
        SysDeptListParams params = new SysDeptListParams();
        params.setLimit(9999);

        PageResult<SysDeptDTO> result = new PageResult<>();
        result.setPageSize(params.getLimit());
        result.setCurPage(params.getPage());

        List<SysDept> treeList = sysDeptBLL.getTreeList();

        result.setTotal(treeList.size());

        if (!CollectionUtils.isEmpty(treeList)) {

            Map<String,String> deptNames = new HashMap<>();
            for (SysDept sysDept : treeList) {
                deptNames.put(sysDept.getDeptId(),sysDept.getName());
            }

            SysDeptDTO dto = null;
            for (SysDept bean : treeList) {
                dto = new SysDeptDTO();
                BeanUtils.copyProperties(bean,dto);
                dto.setParentName(deptNames.get(dto.getParentId()));
                listDTO.add(dto);
            }
        }
        result.setList(listDTO);
        return result;
    }

    @Override
    public PageResult<SysDeptDTO> getList(SysDeptListParams params) {
        List<SysDeptDTO> listDTO = new ArrayList<>();

        PageResult<SysDeptDTO> result = new PageResult<>();
        result.setPageSize(params.getLimit());
        result.setCurPage(params.getPage());
        result.setList(listDTO);

        Page<SysDept> page = sysDeptBLL.getList(params);

        List<SysDept> list = page.getRecords();
        result.setTotal(page.getTotal());

        if (!CollectionUtils.isEmpty(list)) {
            SysDeptDTO dto = null;
            for (SysDept bean : list) {
                dto = new SysDeptDTO();
                BeanUtils.copyProperties(bean,dto);
                listDTO.add(dto);
            }
        }
        return result;
    }

    @Override
    public List<SysDeptListVO> getTreeList() {
        List<SysDeptListVO> listDTO = new ArrayList<>();
        List<SysDept> list = sysDeptBLL.getList();


        if (!CollectionUtils.isEmpty(list)) {
            listDTO = TreeUtils.generateTrees(BeanUtils.copyProperties(list,SysDeptListVO.class));
        }

        return listDTO;
    }

    @Override
    public Boolean del(String id) {
        String operUserId = (String)RequestHolder.get(RequestHolder.OPER_USER_ID);

        Boolean isDel = false;
        try {
            isDel = sysDeptBLL.remove(operUserId, id);
        }catch (Exception e){
            logger.error("部门删除失败");
            e.printStackTrace();
        }
        if (!isDel){
            throw new FrameException(ExceptionEnum.DATA_DELETE_FAILE);

        }

        return isDel;
    }

    @Override
    public String create(SysDeptParams sysDeptParams) {
        String operUser = (String)RequestHolder.get(RequestHolder.OPER_USER_NAME);

        SysDept sysDept = new SysDept();

        BeanUtils.copyProperties(sysDeptParams, sysDept);
        String id = null;
        try {
            id = sysDeptBLL.save(operUser, sysDept);
        }catch (Exception e){
            logger.error("部门保存失败");
            e.printStackTrace();
        }
        if(StringUtils.isEmpty(id)){
            throw new FrameException(ExceptionEnum.DATA_CREATE_FAILE);
        }

        return id;
    }

    @Override
    public Boolean update(SysDeptParams sysDeptParams) {
        String operUser = (String)RequestHolder.get(RequestHolder.OPER_USER_NAME);

        SysDept sysDept = new SysDept();

        BeanUtils.copyProperties(sysDeptParams, sysDept);

        Boolean isUpdate = false;

        try {
            isUpdate = sysDeptBLL.update(operUser, sysDept);
        }catch (Exception e){
            logger.error("部门更新失败");
            e.printStackTrace();
        }

        if(!isUpdate){
            throw new FrameException(ExceptionEnum.DATA_UPDATE_FAILE);
        }

        return isUpdate;
    }


}
